<template>
	<view class="calendar-content">
		<view class="today-box">
			<view class="today">{{ currentYear }}年{{ currentMonth }}月</view>
			<view class="change-box">
				<view class="last-month" @click="clickLast">上月</view>
				<view class="now" @click="clickNowDay">今天</view>
				<view class="next-month" @click="clickNext">下月</view>
			</view>
		</view>
		<view class="date-box">
			<view class="title">
				<view class="title-item" v-for="(item, index) in weekList" :key="index">{{ item.label }}
				</view>
			</view>
			<view class="date-row" v-for="(item, index) in dayList" :key="index">
				<view :class="['date-item']" v-for="(iItem, iIndex) in item" :key="iIndex">
					<view class="edit" v-if="iItem.date && showEditIcon">
						<uni-icons type="compose" color="#c3c7cf"
							@click="clickEdit(iItem, viewDate[iItem.allDate] ?viewDate[iItem.allDate][0] : '')"></uni-icons>
					</view>
					<view :class="['day', isTody(iItem) ? 'now-day' : '']">{{ iItem.date }}</view>
					<view :class="['schedule-name', `name-${viewDate[iItem.allDate][0].classId}`]"
						v-if="viewDate[iItem.allDate]">
						{{ viewDate[iItem.allDate][0].shiftName.split('班')[0] }}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentYear: '',
				currentMonth: '',
				currentDay: '',
				weekList: [{
						label: '日',
						key: ''
					},
					{
						label: '一',
						key: ''
					},
					{
						label: '二',
						key: ''
					},
					{
						label: '三',
						key: ''
					},
					{
						label: '四',
						key: ''
					},
					{
						label: '五',
						key: ''
					},
					{
						label: '六',
						key: ''
					}
				],
				dayList: [],
				viewDate: {}
			}
		},
		props: {
			scheduleData: {
				type: Object,
				default: {}
			},
			showEditIcon: {
				type: Boolean,
				default: true
			}
		},
		watch: {
			scheduleData: {
				handler(newVal, oldVal) {
					this.viewDate = newVal
				}
			}
		},
		created() {
			this.viewDate = this.scheduleData
			// 获取当前年份和月份
			const currentDate = new Date()
			this.currentYear = currentDate.getFullYear()
			this.currentMonth = (currentDate.getMonth() + 1).toString().padStart(2, '0'); // 月份从0开始，所以要加1，并补零
			this.currentDay = currentDate.getDate()
			this.generateCalendar(this.currentYear, this.currentMonth)
		},
		methods: {
			getFullData(data) {
				if (data > 9) return data
				if (data <= 9) return '0' + data
			},
			// 指定月份的天数
			getDaysInMonth(year, month) {
				return new Date(year, month, 0).getDate()
			},
			// 指定月份的第一天是周几
			getFirstDayOfWeek(year, month) {
				return new Date(year, month-1, 1).getDay()
			},
			generateCalendar(year, month) {
				const dayList = []
				const daysInMonth = this.getDaysInMonth(year, month)
				const firstDayOfWeek = this.getFirstDayOfWeek(year, month)
				console.log(daysInMonth,firstDayOfWeek);
				let date = 1
				for (let i = 0; i < 6; i++) {
					dayList.push([])
					for (let j = 0; j < 7; j++) {
						if ((i === 0 && j < firstDayOfWeek) || date > daysInMonth) {
							dayList[i].push({})
						} else {
							dayList[i].push({
								year,
								month: month,
								date: this.getFullData(date),
								allDate: `${year}-${month}-${this.getFullData(date)}`
							})
							date++
						}
					}
					if (date > daysInMonth) {
						break
					}
				}
				this.dayList = dayList
			},
			isTody(item) {
				const currentDate = new Date()
				const nowYear = currentDate.getFullYear()
				const nowMonth = currentDate.getMonth() + 1
				const nowDay = currentDate.getDate()
				if (item.date == nowDay && item.month == nowMonth && item.year == nowYear) {
					return true
				} else {
					return false
				}
			},
			clickLast() {
				this.currentYear = this.currentMonth == '01' ? this.currentYear - 1 : this.currentYear
				this.currentMonth = this.currentMonth == '01' ? 12 : (this.currentMonth - 1).toString().padStart(2, '0')
				this.generateCalendar(this.currentYear, this.currentMonth)
				this.$emit('changeMonth', this.currentYear + '-' + this.currentMonth)
			},
			clickNowDay() {
				const currentDate = new Date()
				this.currentYear = currentDate.getFullYear()
				this.currentMonth = (currentDate.getMonth() + 1).toString().padStart(2, '0')
				this.currentDay = currentDate.getDate()
				this.generateCalendar(this.currentYear, this.currentMonth)
				this.$emit('changeMonth', this.currentYear + '-' + this.currentMonth)
			},
			clickNext() {
				this.currentYear = this.currentMonth == '12' ? this.currentYear + 1 : this.currentYear
				this.currentMonth = this.currentMonth == '12' ? '01' : (Number(this.currentMonth) + 1).toString().padStart(
					2, '0')
				this.generateCalendar(this.currentYear, this.currentMonth)
				this.$emit('changeMonth', this.currentYear + '-' + this.currentMonth)
			},
			clickEdit(item, schedule) {
				this.$emit('chooseSchedule', item.allDate, schedule)
			}
		}
	}
</script>


<style lang="scss" scoped>
	.calendar-content {

		width: 100%;

		.today-box {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 30rpx;

			.today {
				font-size: 32rpx;
				font-weight: bold;
			}

			.change-box {
				display: flex;
				align-items: center;

				.last-month,
				.now,
				.next-month {
					padding: 6rpx 24rpx;
					border: 2rpx solid #cdd0d3;

					&:first-child {
						border-right: none;
					}

					&:last-child {
						border-left: none;
					}
				}
			}
		}

		.date-box {
			background-color: #FFF;

			.title,
			.date-row {
				display: flex;
				height: 130rpx;
				border-bottom: 2rpx solid #f1f1f1;

				.date-item {
					position: relative;
					width: 14.28%;
					display: flex;
					flex-direction: column;
					align-items: center;
					border-right: 2rpx solid #f1f1f1;
					font-weight: 500;

					.day {
						margin: 40rpx 0 0;
					}

					.edit {
						position: absolute;
						top: 0;
						right: 0;
					}

					.now-day {
						padding: 0 12rpx;
						border-radius: 16rpx;
						color: #FFF;
						background-color: #c2801a;
					}

					.schedule-name {
						margin-top: 10rpx;
						font-size: 22rpx;
					}

					.name-1 {
						color: #11be11;
					}

					.name-2 {
						color: #fccb2c;
					}

					.name-3 {
						color: #2dabff;
					}
				}
			}

			.title {
				height: 70rpx;
				align-items: center;

				.title-item {
					width: 14.28%;
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}
		}
	}
</style>